package com.isoft.service.impl;

import com.isoft.entity.DfmeFettEntity;
import com.isoft.service.DfmeFettService;
import com.isoft.service.XmlFileHandler;
import com.isoft.util.XmlParserUtil;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

@Service
public class DfmeFettFileHandler implements XmlFileHandler {
    private static final Logger logger = LoggerFactory.getLogger(DfmeFettFileHandler.class);

    @Resource
    private DfmeFettService dfmeFettService;
    @Resource
    private XmlParserUtil xmlParserUtil;

    @Override
    public boolean supports(Path filePath) {
        String name = filePath.getFileName().toString().toUpperCase();
        return name.contains("FETT") && name.endsWith(".XML");
    }

    @Override
    public String handle(Path filePath) throws Exception {
        String xmlContent = Files.readString(filePath, StandardCharsets.UTF_8);
        DfmeFettEntity entity = xmlParserUtil.parseXml(xmlContent, DfmeFettEntity.class);
        dfmeFettService.parseAndSave(entity);
        logger.info("FETT XML 已成功入库: {}", entity);
        return "FETT XML 已成功入库";
    }
} 